package com.motus.pm.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.motus.pm.service.DeptService;
import com.motus.pm.vo.MotusDept;
import com.motus.pm.vo.MotusEmpInfo;
import com.motus.pm.vo.NewMeetingModel;
import com.motus.pm.vo.PmDeptTask;
import com.motus.pm.vo.PmMeetingEmp;
import com.motus.pm.vo.PmMeetingEmpId;
import com.motus.pm.vo.PmMeetingInfo;
import com.motus.pm.vo.PmMeetingTask;
import com.motus.pm.vo.PmPhaseInfo;
import com.motus.pm.vo.PmProjectInfo;
import com.motus.pm.vo.PmProjectPhase;
import com.motus.pm.vo.PmProjectPhaseId;
import com.motus.pm.vo.PmProjectTask;
import com.motus.pm.vo.modelDriven.NewMeetingInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class DeptAction extends ActionSupport implements
		ModelDriven<PmProjectTask> {
	// {
	// ActionContext ac=ActionContext.getContext();
	// Map<String, Object> session=ac.getSession();
	// if(session.containsKey("deptId")){
	// deptId=(int) session.get("deptId");
	// }
	// }

	private static final long serialVersionUID = 1L;

	// 测试部门编号
	private int deptId;

	// 获取部门任务下发时需要下发的项目编号
	private String pid;
	// 获取部门任务下发时的phaseId
	private String phaseId;
	// 获取任务负责人员的empId
	private int mid;

	@Autowired
	@Qualifier("deptServiceImpl")
	private DeptService deptService;

	private List<PmProjectPhase> projectPhases;

	private List<PmProjectInfo> projectInfos;

	private List<PmPhaseInfo> phaseInfos;

	private List<MotusEmpInfo> empInfos;

	private List<PmProjectTask> projectTasks;

	private List<PmDeptTask> deptTasks;

	private List<PmMeetingTask> meetingTasks;

	private List<PmMeetingInfo> meetingInfos;

	private List<PmMeetingEmp> meetingEmps;

	@Autowired
	@Qualifier("pmMeetingTask")
	private PmMeetingTask meetingTask;

	@Autowired
	@Qualifier("pmProjectTask")
	private PmProjectTask projectTask;

	// 封装模型驱动
	// @Autowired
	// @Qualifier("newMeetingInfo")
	// private NewMeetingInfo newMeetingInfo;

	// 封装模型驱动
	@Autowired
	@Qualifier("newMeetingModel")
	private NewMeetingModel newMeetingInfo;

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPhaseId() {
		return phaseId;
	}

	public void setPhaseId(String phaseId) {
		this.phaseId = phaseId;
	}

	public int getMid() {
		return mid;
	}

	public void setMid(int mid) {
		this.mid = mid;
	}

	public List<PmProjectPhase> getProjectPhases() {
		return projectPhases;
	}

	public void setProjectPhases(List<PmProjectPhase> projectPhases) {
		this.projectPhases = projectPhases;
	}

	public List<PmProjectInfo> getProjectInfos() {
		return projectInfos;
	}

	public void setProjectInfos(List<PmProjectInfo> projectInfos) {
		this.projectInfos = projectInfos;
	}

	public List<PmPhaseInfo> getPhaseInfos() {
		return phaseInfos;
	}

	public void setPhaseInfos(List<PmPhaseInfo> phaseInfos) {
		this.phaseInfos = phaseInfos;
	}

	public List<MotusEmpInfo> getEmpInfos() {
		return empInfos;
	}

	public void setEmpInfos(List<MotusEmpInfo> empInfos) {
		this.empInfos = empInfos;
	}

	public PmProjectTask getProjectTask() {
		return projectTask;
	}

	public void setProjectTask(PmProjectTask projectTask) {
		this.projectTask = projectTask;
	}

	public List<PmProjectTask> getProjectTasks() {
		return projectTasks;
	}

	public void setProjectTasks(List<PmProjectTask> projectTasks) {
		this.projectTasks = projectTasks;
	}

	public List<PmDeptTask> getDeptTasks() {
		return deptTasks;
	}

	public void setDeptTasks(List<PmDeptTask> deptTasks) {
		this.deptTasks = deptTasks;
	}

	public List<PmMeetingTask> getMeetingTasks() {
		return meetingTasks;
	}

	public void setMeetingTasks(List<PmMeetingTask> meetingTasks) {
		this.meetingTasks = meetingTasks;
	}

	public PmMeetingTask getMeetingTask() {
		return meetingTask;
	}

	public void setMeetingTask(PmMeetingTask meetingTask) {
		this.meetingTask = meetingTask;
	}

	public List<PmMeetingInfo> getMeetingInfos() {
		return meetingInfos;
	}

	public void setMeetingInfos(List<PmMeetingInfo> meetingInfos) {
		this.meetingInfos = meetingInfos;
	}

	public List<PmMeetingEmp> getMeetingEmps() {
		return meetingEmps;
	}

	public void setMeetingEmps(List<PmMeetingEmp> meetingEmps) {
		this.meetingEmps = meetingEmps;
	}

	@Override
	public PmProjectTask getModel() {
		return projectTask;
	}

	// 展示部门项目任务
	@Override
	public String execute() throws Exception {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		projectPhases = deptService.getProjectPhasesByDeptId(deptId);
		deptTasks = deptService.getDeptTasksByDeptId(deptId);
		return super.execute();
	}

	// 部门任务下发界面显示
	public String newProjectTask() {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		projectInfos = deptService.getProjectInfosByDeptId(deptId);
		empInfos = deptService.getEmpInfosByDeptId(deptId);
		return "success";
	}

	// 部门任务分配执行
	public String newProjectTaskExec() {

		pid = projectTask.getPmProjectInfo().getPid();
		phaseId = projectTask.getPmPhaseInfo().getPhaseId();
		int empId = projectTask.getMotusEmpInfoByUserId().getId();
		projectTask.setPmProjectInfo(deptService.getProjectInfoById(pid));
		projectTask.setPmPhaseInfo(deptService.getPhaseInfoById(phaseId));
		projectTask.setMotusEmpInfoByUserId(deptService.getEmpInfosById(empId));
		projectTask.setTaskState(-1);
		deptService.newProjectTask(projectTask);
		return "success";
	}

	// 会议任务下发界面显示
	public String newMeetingTask() {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		deptTasks = deptService.getDeptTasksByDeptIdAndTaskState(deptId, -1);
		empInfos = deptService.getEmpInfosByDeptId(deptId);
		return "success";
	}

	// 会议任务分配执行
	public String newMeetingTaskExec() {
		int deptTaskId = meetingTask.getPmDeptTask().getDeptTaskId();
		int userId = meetingTask.getMotusEmpInfoByUserId().getId();
		meetingTask
				.setMotusEmpInfoByUserId(deptService.getEmpInfosById(userId));
		meetingTask.setPmDeptTask(deptService.getDeptTaskById(deptTaskId));
		deptService.newMeetingTask(meetingTask);
		return "success";
	}

	// 获取项目对应的部门阶段
	public String getPhaseInfosByDeptIdAndPid() {

		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		phaseInfos = deptService.getPhaseInfosByDeptIdAndPid(deptId, pid);
		return "success";
	}

	// 获取项目阶段的起始时间
	public String getProjectPhaseById() {
		projectPhases = new ArrayList<PmProjectPhase>();
		projectPhases.add(deptService.getProjectPhaseById(new PmProjectPhaseId(
				deptService.getProjectInfoById(pid), deptService
						.getPhaseInfoById(phaseId))));
		return "success";
	}

	// 显示部门人员任务列表
	public String showAllEmpTasks() {
		// projectTasks=deptService.getProjectTasksByDeptIdAndTaskState(deptId,
		// projectTask.getTaskState());
		return "success";
	}

	// 处理任务列表展示(一次请求同时处理会议任务和项目任务两种类型)
	public String showAllEmpTasksExec() {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		projectTasks = deptService.getProjectTasksByDeptIdAndTaskState(deptId,
				projectTask.getTaskState());
		meetingTasks = deptService.getMeetingTasksByDeptIdAndTaskState(deptId,
				projectTask.getTaskState());
		return "success";
	}

	public String showUncheckedTasks() {
		return "success";
	}

	// 显示审核任务详情
	public String checkEmpTask() {
		int taskId = projectTask.getTaskId();
		projectTask = deptService.getProjectTaskById(taskId);
		return "success";
	}

	// 确认审核通过
	public String checkEmpTaskExec() {
		int taskId = projectTask.getTaskId();
		int taskRealPoint = projectTask.getTaskRealPoint();
		projectTask = deptService.getProjectTaskById(taskId);
		projectTask.setTaskRealPoint(taskRealPoint);
		// 设置状态为2，代表审核通过
		projectTask.setTaskState(2);
		deptService.updateProjectTask(projectTask);
		return "success";
	}

	// 会议
	public String showDeptMeetings() {

		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		meetingInfos = deptService.getMeetingInfosByDeptId(deptId);
		return "success";
	}

	// 具体会议内容
	public String showDeptMeetingDetail() {
		meetingEmps = deptService.getMeetingEmpsByMid(mid);
		return "success";
	}

	// 新建部门会议
	public String newDeptMeeting() {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		empInfos = deptService.getEmpInfosByDeptId(deptId);
		return "success";
	}

	// 新建部门会议执行动作
	public String newDeptMeetingExec() {
		// 获取部门编号
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> session = ac.getSession();
		deptId = (int) session.get("deptId");

		// 获取并设置meetingInfo对象
		PmMeetingInfo pmMeetingInfo = new PmMeetingInfo();
		pmMeetingInfo.setMname(newMeetingInfo.getMname());
		pmMeetingInfo.setMeetingDate(newMeetingInfo.getMeetingDate());
		pmMeetingInfo.setMeetingType(4);
		pmMeetingInfo.setMeetingContent(newMeetingInfo.getMeetingContent());

		// 获取并设置meetingEmp对象
		Integer[] empIds = newMeetingInfo.getEmpIds();
		Set<PmMeetingEmp> pmMeetingEmps = new HashSet<PmMeetingEmp>();
		for (int empId : empIds) {
			MotusEmpInfo empInfo = deptService.getEmpInfosById(empId);
			PmMeetingEmp pmMeetingEmp = new PmMeetingEmp();
			PmMeetingEmpId pmMeetingEmpId = new PmMeetingEmpId();
			pmMeetingEmpId.setMotusEmpInfo(empInfo);
			pmMeetingEmpId.setPmMeetingInfo(pmMeetingInfo);
			pmMeetingEmp.setId(pmMeetingEmpId);
			pmMeetingEmps.add(pmMeetingEmp);
		}

		// 获取部门对象
		MotusDept dept = deptService.getMotusDeptById(deptId);

		// 获取并设置deptTask对象
		PmDeptTask pmDeptTask = new PmDeptTask();
		pmDeptTask.setDeptTaskName(newMeetingInfo.getTaskName());
		pmDeptTask.setDeptTaskPlanStartTime(newMeetingInfo
				.getTaskPlanStartTime());
		pmDeptTask.setDeptTaskPlanEndTime(newMeetingInfo.getTaskPlanEndTime());
		pmDeptTask.setDeptTaskContent(newMeetingInfo.getTaskContent());
		pmDeptTask.setDeptTaskPoint(newMeetingInfo.getTaskPoint());
		pmDeptTask.setMotusDept(dept);
		pmDeptTask.setPmMeetingInfo(pmMeetingInfo);

		// 设置set
		Set<PmDeptTask> pmDeptTasks = new HashSet<>();
		pmDeptTasks.add(pmDeptTask);

		// 将pmDeptTasks和pmMeetingEmps保存到meetingInfo
		pmMeetingInfo.setPmDeptTasks(pmDeptTasks);
		pmMeetingInfo.setPmMeetingEmps(pmMeetingEmps);

		// 保存meetingInfo对象，通过关联对象关系实现同时保存meetingEmp集合对象以及pmDeptTasks集合对象
		deptService.newDeptMeeting(pmMeetingInfo);
		return "success";
	}

}
